﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using kysec.RAL.EFToMySql;
using System.Data.Linq.SqlClient;

namespace kysec_MVC.Controllers
{
    public class NewsController : Controller
    {
        #region 后台操作显示
        /// <summary>
        /// 实现分页显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult InfoList(int? id, int type)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().RetrieveObject(
                string.Format("News_{0}_{1}", id,type + DateTime.Now.Second));
            if (o == null)
            {
                using (var db = new kysec.RAL.EFToMySql.MySql_kysecContectionString())
                {
                    PagedList<kysec.RAL.EFToMySql.tb_news> orders = (from c in db.tb_news
                                  where c.c_i_classify == type 
                                  orderby c.c_dt_publishDate descending
                                  select c).ToPagedList(id ?? 1, 10); 

                    if (Request.IsAjaxRequest())
                        return PartialView("UCNewsList", orders);
                    db.Dispose();
                    kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().AddObjectWithAbsoluteTime(
                            string.Format("News_{0}_{1}", id, type + DateTime.Now.Second),
                            orders,
                            1,
                            System.Web.Caching.CacheItemPriority.Normal);
                    ViewData["type"] = type;
                    return PartialView(orders);
                }
            }
            return PartialView((PagedList<kysec.RAL.EFToMySql.tb_news>)o);
        }

        /// <summary>
        /// 实现查询数据，根据标题，模糊……
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public ActionResult Search(string title ,int type)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().RetrieveObject(
                string.Format("News_{0}", title));
            if (o == null)
            {
                using (var db = new kysec.RAL.EFToMySql.MySql_kysecContectionString())
                {
                    PagedList<kysec.RAL.EFToMySql.tb_news> orders = (from c in db.tb_news
                                                                     where c.c_i_classify == type && SqlMethods.Like(c.c_vc_title, title + "%")
                                                                     orderby c.c_dt_publishDate descending
                                                                     select c).ToPagedList(1, 10);
                    db.Dispose();
                    kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().AddObjectWithAbsoluteTime(
                        string.Format("News_{0}", title),
                        orders,
                        1,
                        System.Web.Caching.CacheItemPriority.Normal);
                    return PartialView(orders);
                }
            }
            return PartialView((PagedList<kysec.RAL.EFToMySql.tb_news>)o);
        }

        /// <summary>
        /// 返回查询的就结果
        /// </summary>
        [HttpPost]
        public ActionResult SetShow(int? id, int choose, int type)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().RetrieveObject(
                string.Format("News_{0}_{1}_{2}", id, choose, type + DateTime.Now.Second));
            bool isDel = choose == 0 ? true : false;
            if (o == null)
            {
                using (var db = new kysec.RAL.EFToMySql.MySql_kysecContectionString())
                {
                    PagedList<kysec.RAL.EFToMySql.tb_news> orders = (from c in db.tb_news
                                  where c.c_b_isDelete == isDel && c.c_i_classify == type
                                  orderby c.c_dt_publishDate descending
                                  select c).ToPagedList(id ?? 1, 10); 

                    if (Request.IsAjaxRequest())
                        return PartialView("UCNewsList", orders);
                    db.Dispose();
                    kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().AddObjectWithAbsoluteTime(
                            string.Format("News_{0}_{1}_{2}", id, choose, type + DateTime.Now.Second),
                            orders,
                            1,
                            System.Web.Caching.CacheItemPriority.Normal);
                    ViewData["type"] = type;
                    return PartialView(orders);
                }
            }
            return PartialView((PagedList<kysec.RAL.EFToMySql.tb_news>)o);
        }

        public ActionResult AddNews()
        {
            return View();
        }
        /// <summary>
        /// 添加新闻基本信息
        /// </summary>
        [ValidateInput(false)][HttpPost]
        public ActionResult AddNews(tb_news model,string type)
        {
            string result = "0";            
            model.c_i_classify = Convert.ToInt32(type);
            if (model.c_vc_title != null && model.c_ntx_content != null)
            {
                result = kysec.Bll.News.news.AddNews(model);

                if (result.Equals("1"))
                {
                    Response.Redirect(kysec.CR.MyUrl.WebData.WEBURL + "/News/AddNews?type=" + type);
                    return Content("<script type='text/javascript'>alert('添加成功!')</script>");
                }
            }
            else {
                Response.Redirect(kysec.CR.MyUrl.WebData.WEBURL + "/News/InfoList?type=" + type);
                return Content("<script type='text/javascript'>alert('标题或内容不能为空！')</script>");

            }
            return View();
        }

        /// <summary>
        /// 查询数据信息和修改数据信息
        /// </summary>
        public ActionResult UpdateNews(string id,int type,bool isUpdate)
        {            
            List<tb_news> list = kysec.Bll.News.Search.getNewsInfo(id, type);

            if (isUpdate)
                return View(list);
            else
                return PartialView("Search", list);
        }
        /// <summary>
        /// 修改新闻信息
        /// </summary>
        [ValidateInput(false)][HttpPost]
        public ActionResult UpdateNewsInfo(string id, tb_news model,string type)
        {
            string result = "0";

            model.c_i_classify = Convert.ToInt32(type);

            result = kysec.Bll.News.news.UpdateNews(id, model);

            if (result.Equals("1"))
            {
                Response.Redirect(kysec.CR.MyUrl.WebData.WEBURL + "/News/InfoList?type=" + type);
                return Content("<script type='text/javascript'>alert('修改成功!')</script>");
            }
            return View();
        }

 /*       /// <summary>
        /// 设置是否为热点新闻
        /// </summary>
        public string SetHot(Guid newId,string type)
        {
            string result = "0";

            result = kysec.Bll.News.news.SetHotNews(newId);

            if (result.Equals("1"))
                Response.Redirect("/News/InfoList?type=" + type);
            return result;
        }
        */

        /// <summary>
        /// 设置类型的转换
        /// </summary>
        public void SetType(Guid newId,string type)
        {
            string result = "0";

            result = kysec.Bll.News.news.SetTypeNews(newId);

            if (result.Equals("1"))
            {
                Response.Redirect(kysec.CR.MyUrl.WebData.WEBURL + "/News/InfoList?type=" + type);
            }
            else { 
                
            }
        }

        /// <summary>
        /// 删除和恢复新闻
        /// </summary>
        public ActionResult Delete(string id,string type)
        {
            string result = "0";

            result = kysec.Bll.News.news.DeleteNews(id);

            if (result.Equals("1"))
            {
                Response.Redirect(kysec.CR.MyUrl.WebData.WEBURL + "/News/InfoList?type=" + type);
                return Content("<script type='text/javascript'>alert('操作成功!')</script>");
            }
            else {
                return Content("<script type='text/javascript'>alert('操作失败！')</script>");
            }
        }

        /// <summary>
        /// 删除和恢复新闻
        /// </summary>
        public string DeleteOne(string id, string type)
        {
            string result = "0";

            result = kysec.Bll.News.news.DeleteNews(id);

            return result;
        }
        #endregion 后台操作显示

        #region 前台显示
        public ActionResult News(int? id, string type)
        {
            var o = kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().RetrieveObject(
                string.Format("NewsForpre_{0}_{1}", id, type + DateTime.Now.Second));
            if (o == null)
            {
                using (var db = new kysec.RAL.EFToMySql.MySql_kysecContectionString())
                {
                   int t = Int32.Parse(type);
                    PagedList<kysec.RAL.EFToMySql.tb_news> list = (from c in db.tb_news
                                                                     where c.c_b_isDelete == false && c.c_i_classify == t
                                                                     orderby c.c_dt_publishDate descending
                                                                     select c).ToPagedList(id ?? 1, 10); 

                    if (Request.IsAjaxRequest())
                        return PartialView("ucNewForpre", list);
                    db.Dispose();
                    kysec.CR.Cache.CacheManage.DefaultStrategy.GetEntity().AddObjectWithAbsoluteTime(
                            string.Format("NewsForpre_{0}_{1}", id, type + DateTime.Now.Second),
                            list,
                            1,
                            System.Web.Caching.CacheItemPriority.Normal);
                    List<kysec.Bll.News.newsImgData> listImg = kysec.Bll.News.Search.getNewsImg(int.Parse(type), 8, 0);
                    ViewData["ImgData"] = listImg;
                    ViewData["type"] = type;
                    return PartialView(list);
                }
            }
            return PartialView((PagedList<kysec.RAL.EFToMySql.tb_news>)o);
        }
        public ActionResult NewsInfo(string id,string type)
        {
            List<tb_news> list = kysec.Bll.News.Search.getNewsInfo(id, int.Parse(type));
            return View(list);
        }       
        #endregion 前台显示


    }
}
